<?php
namespace Admin\Controller;

use Think\Controller;
use Common\Util\UploadHandler;
use Common\Controller\AdminbaseController;
/**
 * 订单产品类
 *
 */
class ProductController extends AdminbaseController {
    public function index(){
//     	layout('Layout/admin');
    	$ProductModel = D('Product');
    	
    	
    	$key = I('get.key');
    	$where = array('status' => 1);
    	if ($key) {
    		$where ['title'] = array('like', '%'.$key.'%');
    	}
    	
    	$list = $ProductModel->where($where)->select();
    	if ($list) {
    		$contentModel = D('Content');
    		foreach ($list as &$value) {
    			$value ['effects'] = $contentModel->getTargetContent($value ['id']);
    		}
    	}
    	
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function add() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		$ProductModel = D('Product');
    		if ($ProductModel->addProduct(I('post.'))) {
    			
    			$operation = array();
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('添加成功');
    		} else {
    			$this->error($ProductModel->getError());
    		}
    	}
    }
    
    public function edit() {
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$Model = D('Product');
    		
    		$item = $Model->getProductInfo($id);
    		$steps = D('Content')->getTargetContent($id);
    		
    		$this->assign('steps', $steps);
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$id = I ('post.id');
    		
    		$ProductModel = D('Product');
    		if (!$ProductModel->updateInfo(I('post.'))) {
    			$this->error($ProductModel->getError());
    		} else {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('编辑成功');
    		}
    	}
    }
    
    public function delete() {
    	$Model = D('Product');
    	 
    	if (!IS_POST) {
    		$id = I ('get.id');
    		 
    		$result = $OrderModel->where(array('id' => $id))->save(array('status' => 0));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($id);
    			$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    		} else {
    			$this->ajaxReturn(array('status' => 0));
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $Model->where($where)->save(array('status' => 0));
    		} else {
    			$where ['id'] = array('in', implode(',',$ids));
    			$result = $Model->where($where)->save(array('status' => 0));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $productId) {
    			$operation = array('operation_object_id' => $productId);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($productId);
    		}
    		 
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
    
    /**
     * 产品图片上传
     */
    public function uploadProductCover() {
    	$options = C('PRODUCT_FILE_UPLODE_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Product/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name] [0]);
    }
}